package com.txcb.lib.base.utils;

import android.app.Application;
import android.content.Context;
import android.os.Looper;
import android.text.TextUtils;
import android.view.Gravity;
import android.widget.Toast;

/**
 * author Lambert on 2019/10/25
 */
public class ToastUtil {

    public static Context mContext;

    public static void init(Application application) {
        mContext = application;
    }

    /*private控制不应该被实例化*/
    private ToastUtil() {
        throw new UnsupportedOperationException("不能被实例化");
    }

    /**
     * 显示toast
     * @param msg
     */
    public static void showToast(String msg) {
        if (mContext == null) {
            return;
        }
        if (Looper.myLooper() != Looper.getMainLooper()) {
            //只允许主线程弹toast
            return;
        }
        if (!TextUtils.isEmpty(msg)) {
            Toast toast = Toast.makeText(mContext,msg, Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER,0,0);
            toast.show();
        }
    }



    public static void showToast(int msgId) {
        if (mContext == null) {
            return;
        }
        if (Looper.myLooper() != Looper.getMainLooper()) {
            //只允许主线程弹toast
            return;
        }
        if (msgId != -1) {
            Toast toast = Toast.makeText(mContext,msgId, Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER,0,0);
            toast.show();
        }
    }
}
